﻿requirejs.config({
    waitSeconds: 60,
    //文件路径
    paths: {
        jquery: '/lib/jquery/dist/jquery.min',
        jqueryValid: '/lib/jquery-validation/dist/jquery.validate.min',
        jqueryPlaceHolder: '/js/jquery.placeholder.min',
        jqueryValidMethods: '/js/jquery.validate-methods',
        unobtrusive: '/js/jquery.unobtrusive-ajax.min',
        bootstrap: '/js/bootstrap.min',
        tabs: '/js/tabs',
        login: '/js/app/login',
        date: '/laydate/laydate',
        common: '/js/app/common',
        capsLockTip: '/js/jquery.capsLockTip',
        rule: '/js/app/rule',
        ruleLog: '/js/app/ruleLog',
        pointRecord: '/js/app/pointRecord'
    },
    //依赖关系
    shim: {
        'jqueryValid': ['jquery'],
        'jqueryPlaceHolder': ['jquery'],
        'jqueryValidMethods': ['jquery', 'jqueryValid'],
        'unobtrusive': ['jquery'],
        'bootstrap': ['jquery'],
        'tabs': ['jquery'],
        'date': ['jquery'],
        'login': ['jqueryValid'],
        'common': ['jquery'],
        'capsLockTip': ['jquery'],
        'rule': ['jqueryValid'],
        'ruleLog': ['jquery'],
        'pointRecord': ['jqueryValid']
    }
});

require(["jquery", "date", "common", "tabs"], function ($, date, common, tabs) {
    $.ajaxSetup({
        cache: false
    });

    $("a[target=tabs]").click(function () {
        var $this = $(this);
        $this.removeAttr("target");
        common.triggerCreateTabEvent($this.attr("id"), $this.attr("title"), $this.attr("url"));
    });

    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    if (userAgent.toLowerCase().indexOf("trident") > -1 && userAgent.indexOf("rv") > -1) {
        $('input, textarea').click(function () {
            $(this).select();
        });
    }
    if (userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器
        (function ($) {
            var placeholderfriend = {
                focus: function (s) {
                    s = $(s).hide().prev().show().focus();
                    var idValue = s.attr("id");
                    if (idValue) {
                        s.attr("id", idValue.replace("placeholderfriend", ""));
                        $("#" + idValue + "placeholderfriend-error").show();
                    }
                    var clsValue = s.attr("class");
                    if (clsValue) {
                        s.attr("class", clsValue.replace("placeholderfriend", ""));
                    }
                }
            }

            //判断是否支持placeholder
            if (!common.isPlaceholer()) {
                $(function () {
                    // 使IE8兼容placeholder属性
                    $('input[type="text"][placeholder],textarea[placeholder]').each(function () {
                        var that = $(this),
                        text = that.attr('placeholder');
                        if (that.val() === "") {
                            that.val(text).addClass('placeholder');
                        }
                        that.focus(function () {
                            if (that.val() === text) {
                                that.val("").removeClass('placeholder');
                            }
                        })
                        .blur(function () {
                            if (that.val() === "") {
                                that.val(text).addClass('placeholder');
                            }
                        })
                        .closest('form').submit(function () {
                            if (that.val() === text) {
                                that.val('');
                            }
                        });
                    });

                    var form = $(this);
                    var elementsPass = form.find("input[type='password'][placeholder]");
                    elementsPass.each(function (i) {
                        var s = $(this);
                        var pValue = s.attr("placeholder");
                        var sValue = s.val();
                        if (pValue) {
                            if (sValue == '') {
                                var html = this.outerHTML || "";
                                html = html.replace(/\s*type=(['"])?password\1/gi, " type=text placeholderfriend").replace(/\s*(?:value|^on[a-z]+|name)(=(['"])?\S*\1)?/gi, " ").replace(/\s*placeholderfriend/, " placeholderfriend value='" + pValue + "' " + "onfocus='placeholderfriendfocus(this);' ");
                                var idValue = s.attr("id");
                                if (idValue) {
                                    s.attr("id", idValue + "placeholderfriend");
                                }
                                var clsValue = s.attr("class");
                                if (clsValue) {
                                    s.attr("class", clsValue + "placeholderfriend");
                                }
                                s.hide();
                                s.after(html);
                            }
                        }
                    });

                    elementsPass.blur(function () {
                        var s = $(this);
                        var sValue = s.val();
                        if (sValue == '') {
                            var idValue = s.attr("id");
                            if (idValue) {
                                s.attr("id", idValue + "placeholderfriend");
                            }
                            var clsValue = s.attr("class");
                            if (clsValue) {
                                s.attr("class", clsValue + "placeholderfriend");
                            }
                            if (s.next().hasClass("error")) {
                                s.hide().next().next().show();
                                s.hide().next().remove();
                            } else {
                                s.hide().next().show();
                            }
                        }
                        else {
                            var idValue = s.attr("id");
                            if (idValue) {
                                s.next().attr("id", idValue + "placeholderfriend");
                                $("#" + idValue + "placeholderfriend-error").hide();
                            }
                        }
                    });

                    elementsPass.keydown(function () {
                        var s = $(this);
                        var idValue = s.attr("id");
                        if (idValue) {
                            $("#" + idValue + "placeholderfriend-error").hide();
                        }
                    });
                });
            }
            window.placeholderfriendfocus = placeholderfriend.focus;
        })(jQuery);
    };

    $(".search-btn").click(function () {
        var text = '交易';
        var url = window.location.href;
        if (url.indexOf('/report/list') > -1)
            text = '';
        if (url.indexOf('/PointExchangeSetting/List') > -1)
            return true;
        if (!common.checkDate(text))
            return false;
        //return common.checkSqlInject();
        return true;
    });

    $('.input-clear').change(function (event) {
        if ($(this).val() == '') {
            $(this).prev().css('display', 'none');
        } else {
            $(this).prev().css('display', 'inline');
        }
    });

    $('.clear').click(function (event) {
        $(this).next().val('');
        $(this).css('display', 'none');
        $(this).next().focus();
    });

    $(document).ready(function () {
        $(".input-clear").each(function () {
            if ($(this).val() == '' || $(this).val() == "支持模糊搜索") {
                $(this).prev().css('display', 'none');
            } else {
                $(this).prev().css('display', 'inline');
            }
        });
    });

    $('input.export').click(function () {
        $("input#ExportType").val($(this).val());
    });

    $('button#exportButtonActual').click(function () {
        $('#export').modal('hide');
        return true;
    });
})